package sjtu.ist.warehouse4ws.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import sjtu.ist.warehouse4ws.service.PrivilegeService;
import sjtu.ist.warehouse4ws.service.ProcessService;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShowProcessListAction extends ActionSupport {

	private ProcessService processService;
	private PrivilegeService privilegeService;
	private List<sjtu.ist.warehouse4ws.model.Process> processList = new ArrayList<sjtu.ist.warehouse4ws.model.Process>();
	
	//获得Process列表
	public String showProcessList(){
		List<sjtu.ist.warehouse4ws.model.Process> templist=processService.getProcessList();
		this.processList = new ArrayList<sjtu.ist.warehouse4ws.model.Process>();
		
		ActionContext actionContext=ActionContext.getContext();
		Map session=actionContext.getSession();		
		int userType=(Integer)(session.get("user_type"));
		String userCompany=session.get("user_company").toString();		
		
		for(sjtu.ist.warehouse4ws.model.Process p : templist){
			if(privilegeService.checkPrivilege(p.getFileId(),userCompany,userType, "read")){
				processList.add(p);
			}
		}	
		
		return SUCCESS;
	}
	
	@JSON(name = "totalProcess")
	public int getTotalProcess() {
		return this.processList.size();
	}

	@JSON(name = "Process")
	public List<sjtu.ist.warehouse4ws.model.Process> getProcessList() {
		return this.processList;
	}
	
	// 注入ProcessService
	public void setProcessService(ProcessService processService) {
		this.processService = processService;
	}

	public void setPrivilegeService(PrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}
}